package com.example.zhu95.mytimber.entity

import org.litepal.crud.DataSupport

/**
 * @author zhu95
 * @date 2018/3/3 19:10
 *
 */
data class SongPlayCount(
        val songid: Int
) : DataSupport() {
    /**
     * 从1970至今，这是第几周
     */

    var currentWeek: Int = 0
    var score: Int = 0
        private set
    var week0: Int = 0
    var week1: Int = 0
    var week2: Int = 0
    var week3: Int = 0
    var week4: Int = 0
    var week5: Int = 0
    var week6: Int = 0
    var week7: Int = 0
    var week8: Int = 0
    var week9: Int = 0
    var week10: Int = 0
    var week11: Int = 0
    var week12: Int = 0
    var week13: Int = 0
    var week14: Int = 0
    var week15: Int = 0
    var week16: Int = 0
    var week17: Int = 0
    var week18: Int = 0
    var week19: Int = 0
    var week20: Int = 0
    var week21: Int = 0
    var week22: Int = 0
    var week23: Int = 0
    var week24: Int = 0
    var week25: Int = 0
    var week26: Int = 0
    var week27: Int = 0
    var week28: Int = 0
    var week29: Int = 0
    var week30: Int = 0
    var week31: Int = 0
    var week32: Int = 0
    var week33: Int = 0
    var week34: Int = 0
    var week35: Int = 0
    var week36: Int = 0
    var week37: Int = 0
    var week38: Int = 0
    var week39: Int = 0
    var week40: Int = 0
    var week41: Int = 0
    var week42: Int = 0
    var week43: Int = 0
    var week44: Int = 0
    var week45: Int = 0
    var week46: Int = 0
    var week47: Int = 0
    var week48: Int = 0
    var week49: Int = 0
    var week50: Int = 0
    var week51: Int = 0

    init {
        currentWeek = initCurrentWeek()
    }


    public fun calculateScore(): Int {
        /**
         * 当前周在记录的周数中应为什么下标
         */
        val weekindex = currentWeek % weeks
        for (i in 0..(weeks - 1)) {
            val field = this.javaClass.getDeclaredField("week" + i)
            val count = field.get(this) as Int
            var weekScore = 0
            if (weekindex >= i) {
                weekScore = count * (weeks + i - weekindex)
            } else {
                weekScore = count * (i - weekindex)
            }
            score += weekScore
        }
        return score
    }

    companion object {
        /**
         * 记录的周数，52周大概为一年
         */
        const val weeks = 52

        fun getRecordWeeks(): Int {
            return weeks
        }

        private fun initCurrentWeek(): Int {
            val currentTimeMillis = System.currentTimeMillis()
            val weekMillis = 7 * 24 * 60 * 60 * 1000
            return (currentTimeMillis / weekMillis).toInt()
        }
    }
}